package com.excellence.controller;


import com.excellence.datamodel.Result;
import com.excellence.pojo.KnowledgeBase;
import com.excellence.service.KnowledgeBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * @author 徐瑞琪
 */
@Slf4j
@RestController
@RequestMapping("/api/knb")
public class KnowledgeBaseController {

    @Autowired
    KnowledgeBaseService knowledgeBaseService;

    //添加知识
    @PostMapping("/add")
    Result insert(KnowledgeBase record, @RequestParam(value = "files", required = false) List<MultipartFile> files) {
        log.info("新增操作，实体为：{}，文件为{}", record, files);
        knowledgeBaseService.insert(record, files);
        return Result.success(200, "添加成功");
    }

    //根据ID查找知识
    @GetMapping("/selectById/{id}")
    Result selectById(@PathVariable Integer id) {
        log.info("查找操作，查找的ID为{}", id);
        KnowledgeBase res = knowledgeBaseService.selectById(id);
        if (res == null) {
            return Result.success(200, "暂无知识，请添加后重试！！！");
        }
        return Result.success(200, "获取成功", res);
    }

    //根据类型查找知识
    @GetMapping("/selectByType")
    Result selectByKnbType(String knType) {
        log.info("查找操作，查找的知识类型为{}", knType);
        List<KnowledgeBase> res = knowledgeBaseService.selectByKnType(knType);
        log.info("{}", res);
        return Result.success(200, "获取成功", res);
    }

    //查找所有知识
    @GetMapping("/selectAll")
    Result selectAll() {
        List<KnowledgeBase> res = knowledgeBaseService.selectAll();
        log.info("查看所有操作，查看所有知识");
        return Result.success(200, "获取成功", res);
    }

    //删除知识
    @DeleteMapping("/delete/{id}")
    Result deleteById(@PathVariable Integer id) {
        log.info("删除操作，删除的ID为:{}", id);
        knowledgeBaseService.deleteById(id);
        return Result.success(200, "删除成功");
    }

    @PostMapping("/update")
    Result update(KnowledgeBase record, @RequestParam(value = "files", required = false) List<MultipartFile> files) {
        log.info("修改操作，实体为：{}，文件为{}", record, files);
        knowledgeBaseService.updateHaveFile(record, files);
        return Result.success(200, "修改成功");
    }

}
